﻿package net.hainan.chat;



import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.packet.Message;

import android.app.Activity;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.scenery.R;
/****
 * 点击好友后进入的聊天界面实现
 * 和聊天中接收到不同命令后所执行的内容
 * */
public class ChatRoom extends Activity implements OnMessageListener,
		OnClickListener
{
	public static int select;
	public static Double d1;
	public static Double d2;
	private MessageReceiver oUpdateMessage;
	private String oContactAccount;
	private Thread othread;
	private EditText metMessageList;
	private EditText metMessage;
	private Chat oChat;
	private LocationManager locationManager;


	@Override
	protected void onDestroy()
	{
		Openfire.oChattingContactMap.put(oContactAccount, false);
		oUpdateMessage.flag = false;
		oUpdateMessage.oCollector.cancel();
		super.onDestroy();
	}

	public void onClick(View view)
	{
		String msg = metMessage.getText().toString();	
		if (!"".equals(msg))
		{
			try
			{
				oChat.sendMessage(msg);//发送消息
				metMessage.setText("");
				metMessageList.append("我：\n");
				metMessageList.append(msg + "\n\n");
			}
			catch (Exception e)
			{
				// TODO: handle exception
			}
		}
		if("五公祠".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=1;
		}
		if("万绿园".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=2;
		}
		if("火山口".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=3;
		}
		if("假日海滩".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=4;
		}
		if("海瑞墓".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=5;
		}
		if("南天一柱".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=6;
		}
		if("热带野生动植物园".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=7;
		}
		if("鹿回头".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=8;
		}
		if("神州第一泉".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=9;
		}
		if("蜈支洲岛".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=10;
		}
		if("月牙湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=11;
		}
		if("亚龙湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=12;
		}
		if("大东海".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=13;
		}
		if("西岛".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=14;
		}
		if("博鳌论坛".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=15;
		}
		if("石梅湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=16;
		}
		if("太平山瀑布".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=17;
		}
		if("中华民族文化村".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=18;
		}
		if("万泉河".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=19;
		}
		if("五指山大峡谷漂流".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=20;
		}
		if("琼崖公学纪念亭".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=21;
		}
		if("热带雨林".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=22;
		}
		if("冯白驹故居".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=23;
		}
		if("文昌孔庙".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=24;
		}
		if("红色娘子军".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=25;
		}
		if("吊罗山".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=26;
		}
		if("七星岭".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=27;
		}
		if("香水湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=28;
		}
		if("玉蟾宫".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=29;
		}
		if("白石岭".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=30;
		}
		if("枫果山瀑布".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=31;
		}
		if("南山寺".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=32;
		}
		if("文笔峰".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=33;
		}
		if("东郊椰林".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=34;
		}
		if("天堂森林公园".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=35;
		}

	}

	private void sendMsg(String from, String msg)
	{
		if ("#friendposition".equals(msg.toLowerCase()) || "#好友位置".equals(msg))
		{
			try
			{

				metMessageList.append("自动回复："
						+ Openfire.oUtil.getLeftString(from, "@") + "\n");

				Criteria criteria = new Criteria();
				// 获得最好的定位效果
				criteria.setAccuracy(Criteria.ACCURACY_FINE);
				criteria.setAltitudeRequired(false);
				criteria.setBearingRequired(false);
				criteria.setCostAllowed(false);
				// 使用省电模式
				criteria.setPowerRequirement(Criteria.POWER_LOW);
				// 获得当前的位置提供者
				String provider = locationManager.getBestProvider(criteria,
						true);
				// 获得当前的位置
				Location location = locationManager
						.getLastKnownLocation(provider);
				// 获得当前位置的纬度
				Double latitude = location.getLatitude();
				// 获得当前位置的经度
				Double longitude = location.getLongitude();
				oChat.sendMessage("#longitude:" + longitude + "#latitude:"
						+ latitude);
				/*把经纬度信息转成google地图可识别的类型*/
				  String sub="#longitude:";//获取经度数据
				  //int a=msg.indexOf(sub);
				 // if(a>=0){
					 String com=msg.substring(0,sub.length());
					 if("#longitude:".equals(com))
					  {
						// String value="#latitude:34.567:#longitude:354.673";
							String[] names =msg.split("\\:");
							//for(int i=0;i<names.length;i++)
							//{
								d1 = Double.parseDouble(names[1]);
								d2 = Double.parseDouble(names[3]);
								select=0;
							//}
					  }
				 // }

			}
			catch (Exception e)
			{

			}
		}
		if("wugong".equals(msg.toLowerCase())||"五公祠".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=1;
		}
		if("wanglv".equals(msg.toLowerCase())||"万绿园".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=2;
		}
		if("火山口".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=3;
		}
		if("假日海滩".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=4;
		}
		if("海瑞墓".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=5;
		}
		if("南天一柱".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=6;
		}
		if("热带野生动植物园".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=7;
		}
		if("鹿回头".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=8;
		}
		if("神州第一泉".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=9;
		}
		if("蜈支洲岛".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=10;
		}
		if("月牙湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=11;
		}
		if("亚龙湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=12;
		}
		if("大东海".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=13;
		}
		if("西岛".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=14;
		}
		if("博鳌论坛".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=15;
		}
		if("石梅湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=16;
		}
		if("太平山瀑布".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=17;
		}
		if("中华民族文化村".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=18;
		}
		if("万泉河".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=19;
		}
		if("五指山大峡谷漂流".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=20;
		}
		if("琼崖公学纪念亭".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=21;
		}
		if("热带雨林".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=22;
		}
		if("冯白驹故居".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=23;
		}
		if("文昌孔庙".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=24;
		}
		if("红色娘子军".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=25;
		}
		if("吊罗山".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=26;
		}
		if("七星岭".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=27;
		}
		if("香水湾".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=28;
		}
		if("玉蟾宫".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=29;
		}
		if("白石岭".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=30;
		}
		if("枫果山瀑布".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=31;
		}
		if("南山寺".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=32;
		}
		if("文笔峰".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=33;
		}
		if("东郊椰林".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=34;
		}
		if("天堂森林公园".equals(msg))
		{
			Intent intent = new Intent(this, Spots.class);
			startActivity(intent);
			select=35;
		}

		else
		{
			metMessageList.append(Openfire.oUtil.getLeftString(from, "/") + ":\n");
			metMessageList.append(msg + "\n\n");
			
		}
	}

	public void processMessage(Message message)
	{
		sendMsg(message.getFrom(),message.getBody());//收听其他人的回复
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chatroom);
		metMessageList = (EditText) findViewById(R.id.etMessageList);
		metMessage = (EditText) findViewById(R.id.etMessage);
		Button btnSend = (Button) findViewById(R.id.btnSend);
		btnSend.setOnClickListener(this);

		oContactAccount = getIntent().getStringExtra("contactAccount");
		String msg = getIntent().getStringExtra("msg");
		if (msg != null)
		{
			sendMsg(oContactAccount, msg);
		}

		setTitle(oContactAccount);
		oUpdateMessage = new MessageReceiver(oContactAccount);
		oUpdateMessage.setOnMessageListener(this);
		othread = new Thread(oUpdateMessage);
		othread.start();
		ChatManager chatmanager = Openfire.oConnection.getChatManager();
		oChat = chatmanager.createChat(oContactAccount, null);
		locationManager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
	}

}